/***************************************************************************
 *   Copyright (C) 2009 by Zheng Yonglei
 *   zhengyonglei@gmail.com   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "Options.h"

Options::Options():
	isPaused(false),
	language("English"),
	lockSwitchTask(true),
	lastAccount(""),
	hasLogined(false),
	autoSpeak(true),
	enableSpeak(true),
	lockSpeak(false),
	useQSound(false),
	playSoundCommand("mplayer"),
	speakLibPath("./WyabdcRealPeopleTTS"),
	//SpellTestHintDict(DictNames::GRERedBible),
	//grossPassRateStep(0.02),
	quickSelectWordCount(50),
	selectWordPolicy(0),
	scanRangePolicy(0),
	mainWindowStatus(Qt::WindowNoState),
	mainWindowHeight(600),
	mainWindowWidth(800),
	mainWindowX(100),
	mainWindowY(100),
	//browseDisplayFontSize(12),
	//reciteDisplayFontSize(12),
	skipFinishedWords(false),
	forceRefresh(false),
	refreshInterval(0),
	refreshTimer(NULL),
	useYesNoTestMethod(true),
	useMultiExpTestMethod(true),
	useMultiEngTestMethod(true),
	useSpellTestMethod(true),
	curTestMethod(yes_no_method),
	firstGroupWaitSecMax(10),
	secondGroupWaitSecMin(2),
	enableArticleNav(false),
	lockArticleNav(false),
	//showArticleNavList(true),
	//showThNavList(false),
	enableHistoryNav(false),
	lockHistoryNav(false),
	//enableSearch(true),
	//lockSearch(false),
	enableAssociateMap(true),
	hasAssociateMap(false),
	enableWordBrowserSearch(false),
	mainShadingColor("#ffffff"),
	selectShadingColor("#ffffff")
	//ignorePhrase(true)
{
	readSettings();
}

Options::~Options()
{
	//cout<<"Options::~Options()"<<endl;
	writeSettings();
}

void Options::readSettings()
{
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );
	settings.beginGroup( "Global" );
	QVariant val;

	val = settings.value("Language");
	if(val.isValid()) language = val.toString();

	val = settings.value("LastAccount");
	if(val.isValid()) lastAccount = val.toString();

	val = settings.value("RefreshRate");
	if(val.isValid()) refreshInterval = val.toInt();

	val = settings.value("ForceRefresh");
	if(val.isValid()) forceRefresh = val.toBool();

	val = settings.value("ProgramFont");
	if(val.isValid())
		if (!programFont.fromString(val.toString()))
		{
			cout<<"Options::readSettings-->Invalid font descriptor string: "<<val.toString().toUtf8().data()<<endl;
			assert(0);
		}

	val = settings.value("AutoSpeak");
	if(val.isValid()) autoSpeak = val.toBool();

	val = settings.value("EnableSpeak");
	if(val.isValid()) enableSpeak = val.toBool();

	val = settings.value("UseQSound");
	if(val.isValid()) useQSound = val.toBool();

	val = settings.value("PlaySoundCommand");
	if(val.isValid()) playSoundCommand = val.toString();

	val = settings.value("SpeakLibPath");
	if(val.isValid()) speakLibPath = val.toString();
	//cout<<"Options::readSettings-->speakLibPath="<<speakLibPath.toUtf8().data()<<endl;
	QDir dir(speakLibPath);
	if (!dir.exists()) speakLibPath.clear();


	settings.endGroup();

	settings.beginGroup( "ReciteAnalysis" );

	val = settings.value("selectWordPolicy");
	if(val.isValid()) selectWordPolicy = val.toInt();

	val = settings.value("scanRangePolicy");
	if(val.isValid()) scanRangePolicy = val.toInt();

	val = settings.value("QuickSelectWordCount");
	if(val.isValid()) quickSelectWordCount = val.toInt();

	//val = settings.value("grossPassRateStep");
	//if(val.isValid()) grossPassRateStep = val.toDouble();

	settings.endGroup();

	settings.beginGroup( "Browse" );
	val = settings.value("SkipFinishedWords");
	if(val.isValid()) skipFinishedWords = val.toBool();

	//val = settings.value("BrowseDisplayFontSize");
	//if(val.isValid()) browseDisplayFontSize = val.toInt();

	settings.endGroup();

	settings.beginGroup( "Recite" );

	//val = settings.value("SpellTestHintDict");
	//if(val.isValid() && !val.isNull()) SpellTestHintDict = val.toString();

	//val = settings.value("ReciteDisplayFontSize");
	//if(val.isValid()) reciteDisplayFontSize = val.toInt();
	val = settings.value("EnableAssociateMap");
	if(val.isValid()) enableAssociateMap = val.toBool();

	val = settings.value("UseYesNoTestMethod");
	if(val.isValid()) useYesNoTestMethod = val.toBool();

	val = settings.value("UseMultiExpTestMethod");
	if(val.isValid()) useMultiExpTestMethod = val.toBool();

	val = settings.value("UseMultiEngTestMethod");
	if(val.isValid()) useMultiEngTestMethod = val.toBool();

	val = settings.value("UseSpellTestMethod");
	if(val.isValid()) useSpellTestMethod = val.toBool();

	val = settings.value("FirstGroupWaitSecMax");
	if(val.isValid()) firstGroupWaitSecMax = val.toInt();

	val = settings.value("SecondGroupWaitSecMin");
	if(val.isValid()) secondGroupWaitSecMin = val.toInt();

	settings.endGroup();

	settings.beginGroup( "WordBrowser" );
	val = settings.value("EnableNavList");
	if(val.isValid()) enableArticleNav = val.toBool();

	val = settings.value("EnablePagNav");
	if(val.isValid()) enableHistoryNav = val.toBool();

	val = settings.value("EnableWordBrowserSearch");
	if(val.isValid()) enableWordBrowserSearch = val.toBool();

	//val = settings.value("ShowThNavList");
	//if(val.isValid()) showThNavList = val.toBool();

	val = settings.value("MainShadingColor");
	if(val.isValid()) mainShadingColor = val.toString();

	val = settings.value("SelectShadingColor");
	if(val.isValid()) selectShadingColor = val.toString();

	val = settings.value("WordBrowserFont");
	if(val.isValid())
		if (!wordBrowserFont.fromString(val.toString()))
		{
			cout<<"Options::readSettings-->Invalid font descriptor string: "<<val.toString().toUtf8().data()<<endl;
			assert(0);
		}

	settings.endGroup();
}

void Options::writeSettings()
{
	QSettings settings("./configure/ReciteGREWords.conf" , QSettings::IniFormat );
	settings.beginGroup( "Global" );
	settings.setValue("Language", language);
	settings.setValue("LastAccount", QString("%1").arg(lastAccount));
	settings.setValue("AutoSpeak", QString("%1").arg(autoSpeak));
	settings.setValue("EnableSpeak", QString("%1").arg(enableSpeak));
	settings.setValue("UseQSound", QString("%1").arg(useQSound));
	settings.setValue("SpeakLibPath", QString("%1").arg(speakLibPath));
	settings.setValue("PlaySoundCommand", playSoundCommand);
	settings.setValue("RefreshRate", QString("%1").arg(refreshInterval));
	settings.setValue("ForceRefresh", QString("%1").arg(forceRefresh));
	settings.setValue("ProgramFont", QString("%1").arg(programFont.toString()));
	settings.endGroup();

	settings.beginGroup("ReciteAnalysis");
	settings.setValue("selectWordPolicy", QString("%1").arg(selectWordPolicy));
	settings.setValue("scanRangePolicy", QString("%1").arg(scanRangePolicy));
	settings.setValue("QuickSelectWordCount", QString("%1").arg(quickSelectWordCount));
	//settings.setValue("grossPassRateStep", QString("%1").arg(grossPassRateStep));
	settings.endGroup();

	settings.beginGroup( "Browse" );
	settings.setValue("SkipFinishedWords", QString("%1").arg(skipFinishedWords));
	//settings.setValue("BrowseDisplayFontSize", QString("%1").arg(browseDisplayFontSize));
	settings.endGroup();

	settings.beginGroup( "Recite" );
	//settings.setValue("SpellTestHintDict", SpellTestHintDict);
	//settings.setValue("ReciteDisplayFontSize", QString("%1").arg(reciteDisplayFontSize));
	settings.setValue("EnableAssociateMap", QString("%1").arg(enableAssociateMap));
	settings.setValue("UseYesNoTestMethod", QString("%1").arg(useYesNoTestMethod));
	settings.setValue("UseMultiExpTestMethod", QString("%1").arg(useMultiExpTestMethod));
	settings.setValue("UseMultiEngTestMethod", QString("%1").arg(useMultiEngTestMethod));
	settings.setValue("UseSpellTestMethod", QString("%1").arg(useSpellTestMethod));
	settings.setValue("FirstGroupWaitSecMax", QString("%1").arg(firstGroupWaitSecMax));
	settings.setValue("SecondGroupWaitSecMin", QString("%1").arg(secondGroupWaitSecMin));
	settings.endGroup();

	settings.beginGroup( "WordBrowser" );
	//settings.setValue("ShowArticleNavList", QString("%1").arg(showArticleNavList));
	//settings.setValue("ShowThNavList", QString("%1").arg(showThNavList));
	settings.setValue("EnableNavList", QString("%1").arg(enableArticleNav));
	settings.setValue("EnablePagNav", QString("%1").arg(enableHistoryNav));
	settings.setValue("EnableWordBrowserSearch", QString("%1").arg(enableWordBrowserSearch));
	settings.setValue("MainShadingColor", mainShadingColor);
	settings.setValue("SelectShadingColor", selectShadingColor);
	settings.setValue("WordBrowserFont", QString("%1").arg(wordBrowserFont.toString()));
	settings.endGroup();
}

